package com.surfilter.redis;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;

import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

/**
 * Created by jstarseven on 2017/7/20.
 */
public class RedisUtils {

    private static String redis_ip = ResourceBundle.getBundle("redis-config").getString("redis.master.ip");
    private static String redis_port = ResourceBundle.getBundle("redis-config").getString("redis.master.port");
    private static String redis_pass = ResourceBundle.getBundle("redis-config").getString("redis.master.password");
    private static String redis_timeout = ResourceBundle.getBundle("redis-config").getString("redis.master.timeout");
    private static Logger logger = LoggerFactory.getLogger(RedisUtils.class);
    public static Jedis jedis;
    public static JedisPool jedisPool;

    static {
        redis_ip = StringUtils.isBlank(redis_ip) ? "127.0.0.1" : redis_ip;
        redis_port = StringUtils.isBlank(redis_port) ? "6379" : redis_port;
        redis_pass = StringUtils.isBlank(redis_pass) ? "" : redis_pass;
        redis_timeout = StringUtils.isBlank(redis_timeout) ? "500000" : redis_timeout;
        jedis = new Jedis(redis_ip, Integer.valueOf(redis_port), Integer.valueOf(redis_timeout));
        if (StringUtils.isNotBlank(redis_pass))
            jedis.auth(redis_pass);

        JedisPoolConfig master_config = new JedisPoolConfig();
        master_config.setMaxTotal(1000);
        master_config.setMaxIdle(1000);
        master_config.setMaxWaitMillis(50000);
        master_config.setTestOnBorrow(true);
        jedisPool = new JedisPool(master_config, redis_ip, Integer.valueOf(redis_port), Integer.valueOf(redis_timeout), redis_pass);
        logger.info("redis config init:\nredis_ip: " + redis_ip + "\nredis_port: " + redis_port + "\nredis_pass: " + redis_pass + "\nredis_timeout: " + redis_timeout);
    }

    /**
     * 向redis对应表中批量插入数据
     *
     * @param table_name
     * @param list
     */
    public static void rpushInsertRedis(String table_name, List list) {
        logger.info("[ " + table_name + " : redis]新增" + list.size() + "条数据 ");
        if (StringUtils.isBlank(table_name)) return;
        long start_time = new Date().getTime(), item_start_time = new Date().getTime();
        Pipeline p = jedisPool.getResource().pipelined();
        for (int i = 0; i < list.size(); i++) {
            p.lpush(table_name, JSONObject.toJSONString(list.get(i)));
            if ((i + 1) % 100000 == 0) {
                p.sync();
                p.clear();
                long item_end_time = new Date().getTime();
                logger.info("[ " + table_name + " : redis]单次插入100000条记录,耗时: " + (item_end_time - item_start_time) + "ms");
                item_start_time = new Date().getTime();
            }
        }
        p.sync();
        p.clear();
        long end_time = new Date().getTime();
        logger.info("[ " + table_name + " : redis]" + list.size() + "条数据插入完成,耗时:" + (end_time - start_time));
    }

    /**
     * 删除redis对应表数据
     *
     * @param table_name
     */
    public static void deleteTable(String table_name) {
        jedisPool.getResource().del(table_name);
    }

    /**
     * 获取redis连接资源
     */
    public static Jedis getSource() {
        return jedisPool.getResource();
    }

    /**
     * 释放redis连接资源
     *
     * @param jedis
     */
    public static void closeSource(Jedis jedis) {
        jedisPool.returnResource(jedis);
    }


    public static void main(String[] args) {
        jedis.set("ioio", "sdas");
        System.out.println(jedis.get("ioio"));
        jedis.del("ioio");
        System.out.println(jedis.get("ioio"));
    }
}
